using System;
using System.Collections.Generic;

namespace Pixume.TestUnits
{
	public enum TestResult
	{
		PASSED,
		FAILED,
		RETEST,
		FINISH,
	}
	public abstract class TestClass
	{
		public abstract string GetName();
		public delegate TestResult TestMethod();
		public Dictionary<string,TestMethod> TestList = new Dictionary<string, TestMethod>();
	}

	public static class TestCenter
	{
		private static TestClass mTestObj;

		public static void LoadTest(TestClass pTestObject)
		{
			mTestObj = pTestObject;
		}

		public static void RunTest ()
		{
			try {
				Console.WriteLine ("===TEST CENTER : {0}===\n", mTestObj.GetName ());

				foreach(string feKey in mTestObj.TestList.Keys)
				{
                    try
                    {
                        ConsoleColor tempColor = Console.ForegroundColor;
                        Console.ForegroundColor = ConsoleColor.DarkCyan;
                        Console.WriteLine("---Start Test {0}---", feKey);
                        Console.ForegroundColor = tempColor;
                        WriteResult(feKey, mTestObj.TestList[feKey]());
                    }
                    catch (Exception e)
                    {
                        WriteResult(feKey, TestResult.FAILED);
                        WriteError(e.Message);
                    }
					Console.WriteLine ();
				}
			} catch (Exception e) {
				WriteError ("Fatal Error, Unable to Load Tests!");
				WriteError ("ensure test class is loaded and testlist contains at least one test");
				WriteError (e.Message);
				return;
			}
			Console.WriteLine ("===END TEST CENTER : {0}===\n", mTestObj.GetName ());
		}
		public static void WriteError(string pTest)
		{
			ConsoleColor tempColor = Console.ForegroundColor;
			Console.ForegroundColor = ConsoleColor.Red;
			Console.WriteLine (pTest);
			Console.ForegroundColor = tempColor;
		}
		public static void WriteResult (string pTest, TestResult pTestResult)
		{
			ConsoleColor tempColor = Console.ForegroundColor;
			Console.ForegroundColor = ConsoleColor.White;
			Console.Write ("{0}[", pTest.PadRight (50, '.'));

			switch (pTestResult) {
			case TestResult.PASSED:
				Console.ForegroundColor = ConsoleColor.Green;
				break;
			case TestResult.FAILED:
				Console.ForegroundColor = ConsoleColor.Red;
				break;
			case TestResult.RETEST:
				Console.ForegroundColor = ConsoleColor.Yellow;
				break;
			case TestResult.FINISH:
				Console.ForegroundColor = ConsoleColor.DarkGray;
				break;
			}

			Console.Write("{0}",pTestResult.ToString ());
			Console.ForegroundColor = ConsoleColor.White;
			Console.Write ("]\n");
			Console.ForegroundColor = tempColor;

		}
	}
}

